package com.ruoyi.demo.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.SM4Utils;
import com.ruoyi.common.utils.SignatureUtils;
import com.ruoyi.demo.domain.EncryptionTable;
import com.ruoyi.demo.mapper.EncryptionTableMapper;
import com.ruoyi.system.domain.SysConfig;
import com.ruoyi.system.service.ISysConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class EncryptionTableServiceImpl extends ServiceImpl<EncryptionTableMapper, EncryptionTable> implements EncryptionTableService {

    @Autowired
    private EncryptionTableMapper encryptionTableMapper;
    @Autowired
    private ISysConfigService iSysConfigService;

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void SM4DataEncrypDecryp() {
        // 加密表数据
        List<EncryptionTable> list = this.list();

        // 获取配置 需要加密或解密
        SysConfig sysConfig = iSysConfigService.selectConfigById(Long.valueOf("100"));
        String configValue = sysConfig.getConfigValue();

        log.info("1仅加密 or 2仅签名 or 3加密签名,{}",configValue);

        for (EncryptionTable tableData : list) {
            List<Map<String, Object>> dataByTableName = encryptionTableMapper.getDataByTableName(tableData.getTableName(), tableData.getTableColumn(),tableData.getIdColumn());

            // 创建StringBuilder 存储签名字段
            StringBuilder sb = new StringBuilder();

            // 拿到未加密的数据
            for (Map<String, Object> stringObjectMap : dataByTableName) {
                // 存储需要修改的字段 key字段
                HashMap<String, Object> map = new HashMap<>();
                stringObjectMap.forEach((key,value) -> {

                    // 如果是主键 则不做更新
                    if(key.equals(tableData.getIdColumn())){
                        return;
                    }

                    switch (configValue){
                        case "1":
                        case "3":
                            // 加密后 存储
                            String newValue = SM4Utils.encrypt(value.toString());
                            map.put(key,newValue);
                            if("3".equals(configValue)){
                                sb.append(value).append("&");
                            }
                            break;
                        case "2":
                            sb.append(value).append("&");
                            break;
                        default:
                            // 全部解除
                            String decrypt = SM4Utils.decrypt(value.toString());
                            map.put(key,decrypt);
                    }
                });

                if(sb.length() > 0 ){
                    // 签名 并 存储
                    String signature = SignatureUtils.createSignature(sb);
                    map.put("signature",signature);
                }else if("0".equals(configValue)){
                    map.put("signature",null);
                }

                // 存入库 encryptionTableMapper
                encryptionTableMapper.updDataByTable(tableData.getTableName(),map,tableData.getIdColumn(),stringObjectMap.get(tableData.getIdColumn()).toString());
            }

        }
    }
}
